package com.shigen.demo.pattern;

/**
 * demo
 *
 * @author shigenfu
 * @date 2022/5/21 8:25 下午
 */
public class SingletonDemo {

  private static volatile SingletonDemo instance = null;

  private SingletonDemo() {
    System.out.println(Thread.currentThread().getName() + "--------构造方法");
  }

  public static SingletonDemo getInstance() {
    if (instance == null) {
      synchronized (SingletonDemo.class)
      {
        if (instance == null) {
          instance = new SingletonDemo();
        }
      }
    }
    return instance;
  }

  public static void main(String[] args) {
    // System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
    // System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
    // System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());

    for (int i = 0; i < 20; i++) {
      new Thread(SingletonDemo::getInstance, String.valueOf(i)).start();
    }


  }


}
